

function testsetA()
	sleep_time = 20000
	wakeup_time = 10000

	send_num = 4
	send_target = 0x0000
	b2s_payload_len = 12
	s2b_payload_len = 24
	printNote()
end


function testsetB()
	sleep_time = 20000
	wakeup_time = 10000
	waik_send_num = 1
	send_num = 2
	send_target = 0x0000
	b2s_payload_len = 12
	s2b_payload_len = 24
	printNote()
end


function printNote()		
	print("这个程序用于测试发送多个包后，进入休眠状态，然后再唤醒，再发送包，看是否能够正常工作")
	print("sender模块运行发送程序，发送%d个包，然后立即休眠%d秒，随后后唤醒，如此循环",
		send_num,sleep_time/1000)
	print("一个模块作为base，收到sender的数据后立即回复")
end
receive = nil
testsetB()

function runB()
	print("开始运行base")
	receive = baseRecv
end

function stopB()
	print("停止base")
	receive = nil
end

function baseRecv(rpkg)	
	if rpkg[1]==string.byte("F") then	
		print("base收到了sleeper %04X发来的F包，SNO = %d，长度为%d",rpkg.addr, rpkg[2],#rpkg)
		pkg={string.byte("R"),rpkg[2]}		
		for i=3,b2s_payload_len do
			pkg[i] = i
		end
		pkg.addr = rpkg.addr
		pkg.port = 48
		send__(pkg)
	end
end
sleep = {}

function sleepRecv(rpkg)
	if rpkg[1]==string.byte("h") and rpkg.port == 102 then
		print("收到握手"..rpkg[2])
		sleep.received_sno = rpkg[2]
		return
	end
	if rpkg[1]==string.byte("R") then		
		print("sleeper收到了%04X发来的R包，SNO = %d，长度为%d",rpkg.addr, rpkg[2],#rpkg)
		return
	end
end

function runS()
	print("开始运行sleeper")
	receive = sleepRecv
	sleep.sno = 1
	sleep.waik_up_sno = 0
	sleep.received_sno = -1

	sleepersLoop1()
end

function stopS()
	print("停止sleeper")
	receive = nil
end



function sleepersLoop1()
	if receive ~= sleepRecv then
		return
	end
	sleep.pkg_sno = 10
	for i=1,send_num do
		pkg={string.byte("F"),sleep.pkg_sno}
		sleep.pkg_sno = sleep.pkg_sno+1		
		for j=3,s2b_payload_len do
			pkg[j] = j
		end
		pkg.addr = send_target
		pkg.port = 48
		send__(pkg)
		print("sleeper发送了一个包到"..string.format("%04X",send_target))
	end	
	pkg={string.byte("S")}
	pkg.addr = 0xfff0
	pkg.port = 111
	send__(pkg)
	print("第 %d 次睡眠。。。", sleep.sno)
	sleep.sno = sleep.sno+1
	later(sleep_time,sleepersLoop2)
end

function sleepersLoop2()
	if receive ~= sleepRecv then
		return
	end
	print("唤醒！")
	waikup_times = 0
	waikUp()
end

function waikUp()	
	if sleep.received_sno == sleep.waik_up_sno then
		print("唤醒成功！")
		sleep.received_sno = -1
		waikUpSendPkg()
		return
	end
	if(waikup_times>10) then
		print("唤醒失败！")
		print("本次测试结束。")
		return
	end
	if(sleep.waik_up_sno<200) then
		sleep.waik_up_sno = sleep.waik_up_sno+1
	else
		sleep.waik_up_sno = 1
	end
	pkg={string.byte("H"),sleep.waik_up_sno}
	pkg.addr = 0xfff0
	pkg.port = 102
	send__(pkg)
	waikup_times = waikup_times+1
	print("唤醒握手包%d",sleep.waik_up_sno)
	later(100,waikUp)
end

function waikUpSendPkg()
	sleep.pkg_sno = 110
	for i=1,waik_send_num do
		pkg={string.byte("F"),sleep.pkg_sno}
		sleep.spkg_snono = sleep.pkg_sno+1		
		for j=3,s2b_payload_len do
			pkg[j] = j
		end
		pkg.addr = send_target
		pkg.port = 48
		send__(pkg)
		print("sleeper发送了一个包到"..string.format("%04X",send_target))
	end	
	later(wakeup_time,sleepersLoop1)
end

function __receive(rpkg)
	if(rpkg==nil)	then
		print("recv nothing")
		return
	end
	if receive==nil then
		body = ""
		for i=1,#rpkg do			
			body = body..string.format("%02X ", rpkg[i])
		end
		print("收到了 %d data %04X->%d: %q",#rpkg,rpkg.addr, rpkg.port,body)
	else
		receive(rpkg)
	end
end